<template>
  <div class="login">
    <div class="login-left">
      <div class="login-title">
        <h2>昌记生活网</h2>
      </div>
    </div>
    <div class="login-right">
      <div>
        <h2>欢迎登录</h2>
        <el-form ref="ruleFormRef" style="max-width: 600px" :model="ruleForm" status-icon :rules="rules"
          label-width="auto" class="demo-ruleForm">
          <el-form-item label="账号" prop="account">
            <el-input v-model="ruleForm.account" autocomplete="off" placeholder="请输入账号" />
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="ruleForm.password" type="password" placeholder="请输入密码" autocomplete="off" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm(ruleFormRef)">
              登录
            </el-button>
            <el-button @click="resetForm(ruleFormRef)">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script setup>
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { adminApi } from '@/apis/login'
const router = useRouter()
const ruleForm = reactive({
  account: 'super',
  password: '88888888',
})
const ruleFormRef = ref(null)
const rules = reactive({
  account: [{ trigger: 'blur' }],
  password: [{ trigger: 'blur' }],
})

const submitForm = (formEl) => {
  if (!formEl) return
  formEl.validate((valid) => {
    if (valid) {
      adminApi.login(ruleForm).then(res => {
        if (res.code == 200) {
          localStorage.setItem('token', res.token)
          adminApi.getUserInfo().then(res2 => {
            if (res2.code == 200) {
              localStorage.setItem('userinfo', JSON.stringify(res2.data))
              router.replace('/analytics')
            }
          })
        }
      })
    } else {
      console.log('error submit!')
    }
  })
}
</script>
<style scoped lang="scss">
.login {
  width: 100vw;
  height: 100vh;
  background: linear-gradient(to right, rgba(105, 140, 240), rgb(126, 77, 185));
  display: flex;

  >div {
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;

    .login-title {
      font-size: 36px;
    }
  }

  .login-left {
    width: calc(100% - 500px);
  }

  .login-right {
    width: 500px;
    background-color: #fff;

    >div {
      h2 {
        margin: 20px;
        color: #000;
        text-align: center;
      }
    }
  }
}
</style>
